package com.yunhai.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.enums.LanuageEnum;
import com.yunhai.common.utils.StringUtils;
import com.yunhai.system.domain.Product;
import com.yunhai.system.domain.ProductType;
import com.yunhai.system.service.ProductService;
import com.yunhai.system.service.ProductTypeService;
import org.apache.commons.collections4.MapUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 产品列表
 *
 * @author jxj
 * @since 2021-01-09 17:53:32
 */
@RestController
@RequestMapping("/api/product")
public class ProductController extends BaseController {
    /**
     * 产品服务
     */
    @Resource
    private ProductService productService;

    /**
     * 产品分类服务
     */
    @Resource
    private ProductTypeService productTypeService;


    /**
     * 获取产品分类
     * @param param
     * @return
     */
    @PostMapping("typeList")
    public AjaxResult selectAll(HttpServletRequest httpServletRequest, @RequestBody Map<String, Object> param) {
        Integer parent = MapUtils.getInteger(param, "parent");
        QueryWrapper<ProductType> productTypeQW = new QueryWrapper<>();
        productTypeQW.orderByDesc("create_time");
        if (null != parent) {
            productTypeQW.eq("parent_id", parent);
        }
        String language = httpServletRequest.getHeader("yh-language");
        if (StringUtils.isBlank(language)) {
            language = LanuageEnum.CH.getCode().toString();
        }
        productTypeQW.eq("language", language);

        return AjaxResult.success(productTypeService.list(productTypeQW));
    }


    /**
     * 根据分类获取产品
     * @param param
     * @return
     */
    @PostMapping("getProductByType")
    public AjaxResult getProductByType(HttpServletRequest httpServletRequest,@RequestBody Map<String, Object> param) {
        String language = httpServletRequest.getHeader("yh-language");
        if (StringUtils.isBlank(language)) {
            language = LanuageEnum.CH.getCode().toString();
        }
        param.put("language", language);
        return AjaxResult.success(productService.productList(param));
    }


    /**
     * 获取产品分类
     * @param param
     * @return
     */
    @PostMapping("info")
    public AjaxResult info(HttpServletRequest httpServletRequest,@RequestBody Map<String, Object> param) {
        Integer parent = MapUtils.getInteger(param, "id");
        if (null == parent) {
            return AjaxResult.error("ID不能为空");
        }
        String language = httpServletRequest.getHeader("yh-language");
        if (StringUtils.isBlank(language)) {
            language = LanuageEnum.CH.getCode().toString();
        }
        param.put("language", language);
        return AjaxResult.success(productService.info(param));
    }


}